<?php

class CategoryController extends Controller
{
	public function actionIndex()
	{
		$model= new CategoryComponent;
		$dataProvider = new CActiveDataProvider('CategoryComponent', array(
                                                'criteria'=>  array(
                                                    'condition'=>"name like '%".$model->name."%'",
                                                ),
                                                'pagination'=>array(
                                                    'pageSize'=>2,
                                                ),
                                                'sort'=>array(
                                                    'defaultOrder'=> array('name'=>false),
                                                )
                                                
                                            ));
        $this->render('index',array(
                'dataProvider'=>$dataProvider,
                'model'=>$model
        ));
	}
	
	public function actionForm($id=null)
	{
		$model= new CategoryComponent;
		if ($id && !($model = CategoryComponent::model()->findByPk($id))) {
		    $this->redirect(Yii::app()->baseUrl.'/category/');
        }
        $catlist= CategoryComponent::getList(array('id'=>$id));
		if(isset($_POST['Category']))
        {
			$this->handleAjaxValidation($model);
            $this->performAjaxValidation($model);
        	$model->attributes = $_POST['Category'];
            if($model->save()) {
                $this->redirect(Yii::app()->baseURL.'/category/');
            }
        }
		
		$this->render('form', array('catlist'=>$catlist,'model'=>$model));
	}
	
	public function actionCreate() {
		$model = new Category();
		if (isset($_POST["txtName"])) {
			$model->name = $_POST["txtName"];
			$model->description = $_POST["txtDesc"];
			$model->save();
			$this->render('index');
		} else {
			echo "bug";
		}
		
		
	}

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}